package com.lee.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPListParseEngine;
import org.apache.commons.net.io.Util;

public class FTPHelper {
	
	// TODO:写在配置文件中  FTP连接参数
	private static final String FTP_SERVER = "202.204.54.39";		// ftp server IP
	private static final String FTP_USER_NAME = "oracle_vm";		// ftp server用户名
	private static final String FTP_PASSWORD = "123456";			// ftp server 密码
	
	//文件对应关系 上传: upload->import  下载：export->download
	// FTP服务器路径 
	public static final String EXPORT_PATH = "/export";		// 导出数据路径，数据：本地->FTP 服务器
	public static final String IMPORT_PATH = "/import";    	// 导入数据路径，数据：FTP服务->本地
	
	// 本地路径
	public static final String UPLOAD_PATH = "F:\\upload";				// 本地文件上传路径，数据：本地->FTP服务器
	public static final String DOWNLOAD_PATH = "F:\\download";	// 本地下载文件路径，数据：FTP服务器->本地
	
	private static FTPClient  ftpClient= null;
	
	public static void uploadFile() {
		boolean res = connectServer(FTP_SERVER, IMPORT_PATH, FTP_PASSWORD);
		if (!res) {
			return;
		}
		FileInputStream inputStream = null;		
		try {
			
			// TODO：根据需要修改
			File file = new File("F:/export/20130620.txt");
			inputStream = new FileInputStream(file);
			// 设置上传目录
			ftpClient.changeWorkingDirectory(IMPORT_PATH);
			ftpClient.setBufferSize(1024);
			ftpClient.setControlEncoding("UTF-8");
			
			// 设置文件类型
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			String fileName = getTime();
			ftpClient.storeFile(fileName+".txt", inputStream);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Util.closeQuietly(inputStream);
			try {
				ftpClient.disconnect();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	
	/**
	 * 连接FTP Server服务器
	 * @param host
	 * @param userName
	 * @param password
	 * @return 是否成功
	 */
	public static  boolean connectServer(String host,String userName,String password) {
		ftpClient = new FTPClient();
		try {
			ftpClient.connect(FTP_SERVER);
			return ftpClient.login(FTP_USER_NAME, FTP_PASSWORD);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 从FTP服务器上将文件下载到本地
	 */
	public static void downloadFile() {
		boolean res = connectServer(FTP_SERVER, IMPORT_PATH, FTP_PASSWORD);
		if (!res) {
			return;
		}
		FileOutputStream outputStream = null;
		try {
			File file = new File(DOWNLOAD_PATH);
			if (!file.exists()) {
				file.mkdir();
			}
			String localFile =DOWNLOAD_PATH + File.separator +getTime() +".txt";
			outputStream = new FileOutputStream(localFile);
			//TODO:根据需要修改
			String remoteFileString = "/export/20130619.txt";
			ftpClient.setBufferSize(1024);
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			ftpClient.retrieveFile(remoteFileString, outputStream);
		} catch (Exception e) {
			e.printStackTrace();
		}  finally {
			Util.closeQuietly(outputStream);
			try {
				ftpClient.disconnect();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	
	public static List<String> getFileList(String directory) {
		boolean connected = connectServer(FTP_SERVER, IMPORT_PATH, FTP_PASSWORD);
		if (!connected) {
			return null;
		}
		try {
			List<String> fileList = new ArrayList<String>(); 
//			ftpClient.changeWorkingDirectory(directory);
			FTPListParseEngine engine = ftpClient.initiateListParsing(directory);
			List<FTPFile> ftpFiles = Arrays.asList(engine.getFiles());
			for (FTPFile ftpFile : ftpFiles) {
				fileList.add(directory+"/"+ftpFile.getName());
				System.out.println(directory+"/"+ftpFile.getName());
			}
			return fileList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static String getTime() {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		return format.format(calendar.getTime());
	}
	
	public static void main(String[] args) {
//		FTPHelper.uploadFile();
//		FTPHelper.downloadFile();
		FTPHelper.getFileList(EXPORT_PATH);
	}

}
